using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

namespace AzureContrib.WindowsAzure.StorageClient
{
	internal class CloudQueueClientWrapper : ICloudQueueClient
	{
		private readonly CloudQueueClient cloudQueueClient;

		public CloudQueueClientWrapper(string baseAddress, StorageCredentials credentials)
		{
			cloudQueueClient = new CloudQueueClient(baseAddress, credentials);
		}

		public CloudQueueClientWrapper(Uri baseAddressUri, StorageCredentials credentials)
		{
			cloudQueueClient = new CloudQueueClient(baseAddressUri, credentials);
		}

		public CloudQueueClientWrapper(Uri baseAddressUri, IStorageCredentials credentials)
		{
			cloudQueueClient = new CloudQueueClient(baseAddressUri, credentials.WrappedCredentials);
		}

		public RetryPolicy RetryPolicy
		{
			get { return cloudQueueClient.RetryPolicy; }
			set { cloudQueueClient.RetryPolicy = value; }
		}

		public TimeSpan Timeout
		{
			get { return cloudQueueClient.Timeout; }
			set { cloudQueueClient.Timeout = value; }
		}

		public TimeSpan ApproximateMessageCountCacheLength
		{
			get { return cloudQueueClient.ApproximateMessageCountCacheLength; }
			set { cloudQueueClient.ApproximateMessageCountCacheLength = value; }
		}

		public bool UsePathStyleUris
		{
			get { return cloudQueueClient.UsePathStyleUris; }
		}

		public IStorageCredentials Credentials
		{
			get { return StorageCredentialsExtensions.Wrap(cloudQueueClient.Credentials); }
		}

		public ICloudQueue GetQueueReference(string queueAddress)
		{
			return new CloudQueueWrapper(cloudQueueClient.GetQueueReference(queueAddress));
		}

		public IEnumerable<ICloudQueue> ListQueues()
		{
			return cloudQueueClient.ListQueues().Select(cloudQueue => new CloudQueueWrapper(cloudQueue));
		}

		public IEnumerable<ICloudQueue> ListQueues(string prefix)
		{
			return cloudQueueClient.ListQueues(prefix).Select(cloudQueue => new CloudQueueWrapper(cloudQueue));
		}

		public IEnumerable<ICloudQueue> ListQueues(string prefix, QueueListingDetails detailsIncluded)
		{
			return cloudQueueClient.ListQueues(prefix, detailsIncluded).Select(cloudQueue => new CloudQueueWrapper(cloudQueue));
		}

		public IResultSegment<ICloudQueue> ListQueuesSegmented()
		{
			return new ResultSegmentWrapper<CloudQueue, ICloudQueue>(cloudQueueClient.ListQueuesSegmented(), CloudQueueWrapper.Convert);
		}

		public IResultSegment<ICloudQueue> ListQueuesSegmented(string prefix, QueueListingDetails detailsIncluded)
		{
			return new ResultSegmentWrapper<CloudQueue, ICloudQueue>(cloudQueueClient.ListQueuesSegmented(prefix, detailsIncluded), CloudQueueWrapper.Convert);
		}

		public IResultSegment<ICloudQueue> ListQueuesSegmented(string prefix, QueueListingDetails detailsIncluded, int maxResults, ResultContinuation continuationToken)
		{
			return new ResultSegmentWrapper<CloudQueue, ICloudQueue>(cloudQueueClient.ListQueuesSegmented(prefix, detailsIncluded, maxResults, continuationToken), CloudQueueWrapper.Convert);
		}

		public IAsyncResult BeginListQueuesSegmented(AsyncCallback callback, object state)
		{
			return cloudQueueClient.BeginListQueuesSegmented(callback, state);
		}

		public IAsyncResult BeginListQueuesSegmented(string prefix, AsyncCallback callback, object state)
		{
			return cloudQueueClient.BeginListQueuesSegmented(prefix, callback, state);
		}

		public IAsyncResult BeginListQueuesSegmented(string prefix, QueueListingDetails detailsIncluded, AsyncCallback callback, object state)
		{
			return cloudQueueClient.BeginListQueuesSegmented(prefix, detailsIncluded, callback, state);
		}

		public IAsyncResult BeginListQueuesSegmented(string prefix, QueueListingDetails detailsIncluded, int maxResults, ResultContinuation continuationToken, AsyncCallback callback, object state)
		{
			return cloudQueueClient.BeginListQueuesSegmented(prefix, detailsIncluded, maxResults, continuationToken, callback, state);
		}

		public IResultSegment<ICloudQueue> EndListQueuesSegmented(IAsyncResult asyncResult)
		{
			return new ResultSegmentWrapper<CloudQueue, ICloudQueue>(cloudQueueClient.EndListQueuesSegmented(asyncResult), CloudQueueWrapper.Convert);
		}

		public event EventHandler<ResponseReceivedEventArgs> ResponseReceived
		{
			add { cloudQueueClient.ResponseReceived += value; }
			remove { cloudQueueClient.ResponseReceived -= value; }
		}
	}
}